/*     */ package org.apache.mina.transport.socket.nio;
/*     */ 
/*     */ import java.net.InetSocketAddress;
/*     */ import java.net.ServerSocket;
/*     */ import java.net.SocketAddress;
/*     */ import java.net.SocketException;
/*     */ import java.nio.channels.SelectionKey;
/*     */ import java.nio.channels.Selector;
/*     */ import java.nio.channels.ServerSocketChannel;
/*     */ import java.nio.channels.SocketChannel;
/*     */ import java.util.Collection;
/*     */ import java.util.Iterator;
/*     */ import java.util.concurrent.Executor;
/*     */ import org.apache.mina.core.polling.AbstractPollingIoAcceptor;
/*     */ import org.apache.mina.core.service.IoProcessor;
/*     */ import org.apache.mina.core.service.TransportMetadata;
/*     */ import org.apache.mina.transport.socket.DefaultSocketSessionConfig;
/*     */ import org.apache.mina.transport.socket.SocketAcceptor;
/*     */ import org.apache.mina.transport.socket.SocketSessionConfig;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ZimbraSocketAcceptor
/*     */   extends AbstractPollingIoAcceptor<NioSession, ServerSocketChannel>
/*     */   implements SocketAcceptor
/*     */ {
/*     */   private volatile Selector selector;
/*     */   private final ServerSocketChannel channel;
/*     */   
/*     */   public ZimbraSocketAcceptor(ServerSocketChannel channel)
/*     */   {
/*  59 */     super(new DefaultSocketSessionConfig(), NioProcessor.class);
/*  60 */     ((DefaultSocketSessionConfig)getSessionConfig()).init(this);
/*  61 */     this.channel = channel;
/*     */   }
/*     */   
/*     */   public ZimbraSocketAcceptor(ServerSocketChannel channel, int processorCount) {
/*  65 */     super(new DefaultSocketSessionConfig(), NioProcessor.class, processorCount);
/*  66 */     ((DefaultSocketSessionConfig)getSessionConfig()).init(this);
/*  67 */     this.channel = channel;
/*     */   }
/*     */   
/*     */   public ZimbraSocketAcceptor(ServerSocketChannel channel, IoProcessor<NioSession> processor) {
/*  71 */     super(new DefaultSocketSessionConfig(), processor);
/*  72 */     ((DefaultSocketSessionConfig)getSessionConfig()).init(this);
/*  73 */     this.channel = channel;
/*     */   }
/*     */   
/*     */   public ZimbraSocketAcceptor(ServerSocketChannel channel, Executor executor, IoProcessor<NioSession> processor) {
/*  77 */     super(new DefaultSocketSessionConfig(), executor, processor);
/*  78 */     ((DefaultSocketSessionConfig)getSessionConfig()).init(this);
/*  79 */     this.channel = channel;
/*     */   }
/*     */   
/*     */   protected void init() throws Exception
/*     */   {
/*  84 */     this.selector = Selector.open();
/*     */   }
/*     */   
/*     */   protected void destroy() throws Exception
/*     */   {
/*  89 */     if (this.selector != null) {
/*  90 */       this.selector.close();
/*     */     }
/*     */   }
/*     */   
/*     */   public TransportMetadata getTransportMetadata()
/*     */   {
/*  96 */     return NioSocketSession.METADATA;
/*     */   }
/*     */   
/*     */   public SocketSessionConfig getSessionConfig()
/*     */   {
/* 101 */     return (SocketSessionConfig)super.getSessionConfig();
/*     */   }
/*     */   
/*     */ 
/*     */   public InetSocketAddress getLocalAddress()
/*     */   {
/* 107 */     return (InetSocketAddress)super.getLocalAddress();
/*     */   }
/*     */   
/*     */   public InetSocketAddress getDefaultLocalAddress()
/*     */   {
/* 112 */     return (InetSocketAddress)super.getDefaultLocalAddress();
/*     */   }
/*     */   
/*     */   public void setDefaultLocalAddress(InetSocketAddress localAddress)
/*     */   {
/* 117 */     setDefaultLocalAddress(localAddress);
/*     */   }
/*     */   
/*     */   public boolean isReuseAddress()
/*     */   {
/*     */     try {
/* 123 */       return this.channel.socket().getReuseAddress();
/*     */     } catch (SocketException e) {}
/* 125 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   public void setReuseAddress(boolean reuseAddress)
/*     */   {
/* 131 */     throw new UnsupportedOperationException();
/*     */   }
/*     */   
/*     */   public int getBacklog()
/*     */   {
/* 136 */     throw new UnsupportedOperationException();
/*     */   }
/*     */   
/*     */   public void setBacklog(int backlog)
/*     */   {
/* 141 */     throw new UnsupportedOperationException();
/*     */   }
/*     */   
/*     */   protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle)
/*     */     throws Exception
/*     */   {
/* 147 */     SelectionKey key = handle.keyFor(this.selector);
/*     */     
/* 149 */     if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
/* 150 */       return null;
/*     */     }
/*     */     
/*     */ 
/* 154 */     SocketChannel ch = handle.accept();
/*     */     
/* 156 */     if (ch == null) {
/* 157 */       return null;
/*     */     }
/*     */     
/* 160 */     return new NioSocketSession(this, processor, ch);
/*     */   }
/*     */   
/*     */   protected ServerSocketChannel open(SocketAddress localAddress) throws Exception
/*     */   {
/* 165 */     boolean success = false;
/*     */     try {
/* 167 */       if (this.channel.isBlocking()) {
/* 168 */         throw new IllegalStateException("ServerSocketChannel is blocking mode");
/*     */       }
/*     */       
/* 171 */       this.channel.register(this.selector, 16);
/* 172 */       success = true;
/*     */     } finally {
/* 174 */       if (!success) {
/* 175 */         close(this.channel);
/*     */       }
/*     */     }
/* 178 */     return this.channel;
/*     */   }
/*     */   
/*     */   protected SocketAddress localAddress(ServerSocketChannel handle) throws Exception
/*     */   {
/* 183 */     return handle.socket().getLocalSocketAddress();
/*     */   }
/*     */   
/*     */   protected int select() throws Exception
/*     */   {
/* 188 */     return this.selector.select();
/*     */   }
/*     */   
/*     */   protected Iterator<ServerSocketChannel> selectedHandles()
/*     */   {
/* 193 */     return new ServerSocketChannelIterator(this.selector.selectedKeys(), null);
/*     */   }
/*     */   
/*     */   protected void close(ServerSocketChannel handle) throws Exception
/*     */   {
/* 198 */     SelectionKey key = handle.keyFor(this.selector);
/*     */     
/* 200 */     if (key != null) {
/* 201 */       key.cancel();
/*     */     }
/*     */     
/* 204 */     handle.close();
/*     */   }
/*     */   
/*     */   protected void wakeup()
/*     */   {
/* 209 */     this.selector.wakeup();
/*     */   }
/*     */   
/*     */   private static class ServerSocketChannelIterator implements Iterator<ServerSocketChannel> {
/*     */     private final Iterator<SelectionKey> iterator;
/*     */     
/*     */     private ServerSocketChannelIterator(Collection<SelectionKey> selectedKeys) {
/* 216 */       this.iterator = selectedKeys.iterator();
/*     */     }
/*     */     
/*     */     public boolean hasNext()
/*     */     {
/* 221 */       return this.iterator.hasNext();
/*     */     }
/*     */     
/*     */     public ServerSocketChannel next()
/*     */     {
/* 226 */       SelectionKey key = (SelectionKey)this.iterator.next();
/*     */       
/* 228 */       if ((key.isValid()) && (key.isAcceptable())) {
/* 229 */         return (ServerSocketChannel)key.channel();
/*     */       }
/*     */       
/* 232 */       return null;
/*     */     }
/*     */     
/*     */     public void remove()
/*     */     {
/* 237 */       this.iterator.remove();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/org/apache/mina/transport/socket/nio/ZimbraSocketAcceptor.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */